package com.ruoyi.vote.admin.controller;

import java.util.Arrays;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.vote.admin.domain.VoteActivity;
import com.ruoyi.vote.admin.domain.bo.VoteActivityBo;
import com.ruoyi.vote.admin.domain.vo.VoteActivityVo;
import com.ruoyi.vote.admin.service.IVoteActivityService;
import com.ruoyi.vote.app.domain.request.VoteActivityReq;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import me.chanjar.weixin.common.util.DataUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动管理
 *
 * @author 筑巢
 * @date 2023-07-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/vote/activity")
public class VoteActivityController extends BaseController {

    private final IVoteActivityService iVoteActivityService;

    /**
     * 查询投票 - 活动管理列表
     */
    @SaCheckPermission("vote:activity:list")
    @GetMapping("/list")
    public TableDataInfo<VoteActivityVo> list(VoteActivityReq bo, PageQuery pageQuery) {
        pageQuery.setOrderByColumn("va.create_time");
        pageQuery.setIsAsc("desc");
        return iVoteActivityService.queryPageList(bo, pageQuery);
    }

    /**
     * 活动详情
     *
     * @param id 主键
     */
    @SaCheckPermission("vote:activity:query")
    @GetMapping("/{id}")
    public R<VoteActivityVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iVoteActivityService.queryById(id));
    }

    /**
     * 同步活动缓存
     * @param id   活动ID
     * @return
     */
    @SaCheckPermission("vote:activity:ranking")
    @Log(title = "活动管理 - 更新排行榜", businessType = BusinessType.OTHER)
    @GetMapping("/update-ranking/{id}")
    public R<Void> updateRanking(@NotNull(message = "活动不能为空") @PathVariable String id){
        return toAjax(iVoteActivityService.calculateRankingByaId(id));
    }

    /**
     * 活动结算(自动结算)
     * @param aid
     * @return
     */
    @Log(title = "活动管理 - 结算", businessType = BusinessType.OTHER)
    @GetMapping("/activitySettlement/{aid}")
    public R<Void> activitySettlement(@NotNull(message = "活动不能为空") @PathVariable String aid){
        return toAjax(iVoteActivityService.activitySettlement(aid));
    }

    /**
     * 新增活动
     */
    @SaCheckPermission("vote:activity:add")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VoteActivityBo bo) {
        if(bo.getBalanceTime().getTime() < DateUtils.getNowDate().getTime()
            || bo.getVoteStartTime().getTime() < DateUtils.getNowDate().getTime()){
            throw new RuntimeException("活动时间必须大于当前时间.");
        }
        return toAjax(iVoteActivityService.insertByBo(bo));
    }

    /**
     * 修改活动
     */
    @SaCheckPermission("vote:activity:edit")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VoteActivityBo bo) {
        VoteActivity activity = BeanUtil.toBean(bo, VoteActivity.class);
        return toAjax(iVoteActivityService.updateByBo(activity));
    }

    /**
     * 删除活动
     *
     * @param ids 主键串
     */
    @SaCheckPermission("vote:activity:remove")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iVoteActivityService.deleteWithValidByIds(Arrays.asList(ids)));
    }


}
